package levels;

import lobster.games.studios.R;
import android.app.Activity;
import android.content.Intent;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Level2 extends Activity implements SensorEventListener{

	private SensorManager sensorManager = null;
	private float axisX;
	private ImageView img;
	private TextView levelText;
	private RelativeLayout windowLayout;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		setContentView(R.layout.level);
		getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

		levelText = new TextView(this);
		levelText = (TextView)findViewById(R.id.leveltext);
		levelText.setText("Level 2");
		
		windowLayout = (RelativeLayout)findViewById(R.id.windowlayout);


		img = new ImageView(this);
		img.setImageResource(R.drawable.window);
		windowLayout.addView(img);
		img.setOnClickListener(new OnClickListener() {

			//@Override
			public void onClick(View v) {
				if(img.getId() == R.drawable.openwindow){
					Intent intent = new Intent(getApplicationContext(), Level3.class);
					startActivity(intent);
				}
			}
		}); 
	}

	@Override
	protected void onResume() {
		super.onResume();
		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	protected void onStop() {
		super.onStop();
		sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
		sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
	}


	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		synchronized (this) {
			switch (event.sensor.getType()){
			case Sensor.TYPE_ACCELEROMETER:
				axisX = event.values[0];
				if(axisX < 0){
					img.setImageResource(R.drawable.openwindow);
				}
				else if(axisX > 0){
					img.setImageResource(R.drawable.window);
				}
				break;

			}
		}

	}


	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}
}

